import NavigationService from '../NavigationService';

import {APP_KEY} from './config';

const TOKEN = `${APP_KEY}-token`,
    UID = `${APP_KEY}-uid`,
    USER_INFO = `${APP_KEY}-userInfo`,
    MOBILE = `${APP_KEY}-mobile`,
    SEARCH_HISTORY = `${APP_KEY}-searchHistory`;
const auth = {
    //设置userInfo
    setUserInfo(data = {}) {
        return storage.save({
            key: USER_INFO,
            data,
            expires: null
        });
    },
    //获取userInfo
    getUserInfo() {
        return storage.load({
            key: USER_INFO,
            autoSync: false
        });
    },
    //删除用户信息
    removeUserInfo() {
        return storage.remove({
            key: USER_INFO
        });
    },
    //设置mobile
    setMobile(mobile = '') {
        return storage.save({
            key: MOBILE,
            data: {
                mobile
            },
            expires: null
        });
    },
    //获取mobile
    getMobile() {
        return storage.load({
            key: MOBILE,
            autoSync: false
        });
    },
    //删除mobile
    removeMobile() {
        return storage.remove({
            key: MOBILE
        });
    },
    //设置搜索历史
    setSearchHistory(searchHistory = []) {
        return storage.save({
            key: SEARCH_HISTORY,
            data: {
                searchHistory
            },
            expires: null
        });
    },
    //获取搜索历史
    getSearchHistory() {
        return storage.load({
            key: SEARCH_HISTORY,
            autoSync: false
        });
    },
    //清空搜索历史
    removeSearchHistory() {
        return storage.remove({
            key: SEARCH_HISTORY
        });
    },
    //跳转登录
    goLogin() {
        if (auth.isInLoginPage()) {
            return;
        }
        auth.setIsInLoginPage(true);
        NavigationService.navigate('login');
    },
    //设置是否在登录页
    isInLoginPage() {
        return global.isInLoginPage;
    },
    //获取是否在登录页
    setIsInLoginPage(flag) {
        return global.isInLoginPage = flag;
    },
    //返回上一页
    goBack(key) {
        NavigationService.goBack(key);
    }
};

export default auth;